import { TaskRule } from "../../../rules/TaskRule";
import { TrackBox } from "./TrackBox";
import { GScrollPane } from "../../../../../com/mui/controls/GScrollPane";
import { ScrollPolicy } from "../../../../../fl/controls/ScrollPolicy";
import { ObjectPool } from "../../../../../com/gengine/utils/pools/ObjectPool";
import { TrackItem } from "./item/TrackItem";
import { Global } from "../../../../../com/gengine/global/Global";
import { TaskInfo } from "../../../model/TaskInfo";
import { GuideTipsManager } from "../../../manager/GuideTipsManager";
import { TaskTargetInfo } from "../../../model/TaskTargetInfo";
import { GuideType } from "../../../rules/GuideType";
import { AIManager } from "../../../scene/ai/AIManager";
import { AIType } from "../../../scene/ai/AIType";
import { ETaskType } from "../../../../../Message/Game/ETaskType";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { DataEvent } from "../../../events/DataEvent";
import { EventName } from "../../../mvc/EventName";
import { Cache } from "../../../cache/Cache";
import { Language } from "../../../../../extend/language/Language";
import { HTMLUtil } from "../../../../../com/gengine/utils/HTMLUtil";
import { TaskActInfo } from "../../../resource/info/TaskActInfo";
import { GuideTips } from "../../guide/GuideTips";
type int = number;
//class TaskTrack
    
    export  class TaskTrack /* flash.display.Sprite */
    {
        constructor()
        {
            
            this._sortRule = TaskRule.getTaskGroupSortNormalRule();
            mouseEnabled = false;
            this._trackBox = new TrackBox();
            this._scrollPane = new GScrollPane();
            this._scrollPane.width = 232;
            this._scrollPane.height = 143;
            this._scrollPane.styleName = "GScrollPane";
            this._scrollPane.verticalScrollPolicy = ScrollPolicy.AUTO;
            this._scrollPane.horizontalScrollPolicy = ScrollPolicy.OFF;
            this._scrollPane.source = this._trackBox;
            this._scrollPane.scrollDrag = false;
            this._scrollPane.focusEnabled = false;
            addChild(this._scrollPane);
            return;
        }

        public initTask(arg1: Array<any>): void
        {
            var loc1=0;
            var loc3=null;
            var loc4=null;
            this.dispose();
            var loc2=arg1.length;
            while (loc1 < loc2) 
            {
                loc4 = arg1[loc1];
                (loc3 = ObjectPool.getObject(TrackItem)).initTask(loc4);
                this._trackBox.addItem(loc3);
                ++loc1;
            }
            this.sort();
            Global.instance.callLater(this.laterInit);
            return;
        }

        /* internal  */laterInit(): void
        {
            this._scrollPane.update();
            return;
        }

        public addTask(arg1: TaskInfo): void
        {
            var loc1=ObjectPool.getObject(TrackItem);
            loc1.initTask(arg1);
            this._trackBox.addItem(loc1);
            Global.instance.callLater(this.addLater);
            return;
        }

        protected addLater(): void
        {
            this._trackBox.validateNow();
            this._scrollPane.update();
            return;
        }

        public updateTask(arg1: TaskInfo): void
        {
            var loc3=null;
            var loc1=0;
            var loc2=this._trackBox.itemNum;
            while (loc1 < loc2) 
            {
                if ((loc3 = this._trackBox.getItemAt(loc1) as TrackItem).taskId == arg1.sTask.code) 
                {
                    loc3.updateTask(arg1);
                    break;
                }
                ++loc1;
            }
            Global.instance.callLater(this.upateLater);
            return;
        }

        public updateHeight(arg1: int): void
        {
            this._scrollPane.height = this._scrollPane.height + arg1;
            this._scrollPane.refreshPane();
            if (this._scrollPane.height < 10) 
            {
                this._scrollPane.visible = false;
            }
            else 
            {
                this._scrollPane.visible = true;
            }
            return;
        }

        /* internal  */upateLater(): void
        {
            this._trackBox.layChildChanged = true;
            this._trackBox.validateNow();
            this._scrollPane.update();
            return;
        }

        public delTask(arg1: int): void
        {
            var loc3=null;
            var loc1=0;
            var loc2=this._trackBox.itemNum;
            while (loc1 < loc2) 
            {
                if ((loc3 = this._trackBox.getItemAt(loc1) as TrackItem).taskId == arg1) 
                {
                    this._trackBox.delItem(loc3);
                    loc3.dispose();
                    ObjectPool.disposeObject(loc3, TrackItem);
                    this._trackBox.validateNow();
                    break;
                }
                ++loc1;
            }
            if (GuideTipsManager.task && GuideTipsManager.task.sTask.code == arg1) 
            {
                GuideTipsManager.hideGuideTips();
            }
            this.sort();
            this._scrollPane.update();
            return;
        }

        public dispose(): void
        {
            var loc1=null;
            while (this._trackBox.itemNum > 0) 
            {
                loc1 = this._trackBox.delItemAt(0) as TrackItem;
                loc1.dispose();
                ObjectPool.disposeObject(loc1, TrackItem);
            }
            this._scrollPane.update();
            return;
        }

        public sort(): void
        {
            Global.instance.callLater(this.sortLater);
            return;
        }

        protected sortLater(): void
        {
            var loc3=null;
            var loc4=0;
            var loc5=null;
            var loc1=0;
            var loc2=(this._trackBox.itemNum - 1);
            while (loc1 < loc2) 
            {
                loc3 = this._trackBox.getItemAt(loc1) as TrackItem;
                loc4 = loc1;
                while (loc4 < loc2 + 1) 
                {
                    loc5 = this._trackBox.getItemAt(loc4) as TrackItem;
                    if (this._sortRule.indexOf(loc3.taskInfo.sTask.group) > this._sortRule.indexOf(loc5.taskInfo.sTask.group)) 
                    {
                        this._trackBox.swapItem(loc3, loc5);
                    }
                    ++loc4;
                }
                ++loc1;
            }
            this._trackBox.layChildChanged = true;
            this._trackBox.validateNow();
            return;
        }

        public guideTips(arg1: TaskInfo): void
        {
            this._task = arg1;
            Global.instance.callLater(this.guideTipsLater);
            return;
        }

        protected guideTipsLater(): void
        {
            var loc1=0;
            var loc3=null;
            if (!this._task) 
            {
                return;
            }
            var loc2=this._trackBox.itemNum;
            while (loc1 < loc2) 
            {
                loc3 = this._trackBox.getItemAt(loc1) as TrackItem;
                if (loc3.taskId == this._task.sTask.code) 
                {
                    if (this.showItemGuide(loc3)) 
                    {
                        return;
                    }
                }
                loc2 = this._trackBox.itemNum;
                ++loc1;
            }
            return;
        }

        /* internal  */onTipsClick(arg1: TaskTargetInfo, arg2: TaskInfo): void
        {
            if (arg1 != null) 
            {
                if (arg2.sTask.guide & GuideType.GuideCollection) 
                {
                    this.guideHide();
                    AIManager.onAIControl(arg1, AIType.AI_Convey);
                }
                else if (arg2.sTask.type != ETaskType._ETaskTypeApplyGuild) 
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.TaskAutoPathReq, {"target": arg1, "task": arg2}));
                }
                else 
                {
                    this.guideHide();
                    Dispatcher.dispatchEvent(new DataEvent(EventName.TaskAutoPathReq, {"target": arg1, "task": arg2}));
                }
            }
            return;
        }

        public guideHide(): void
        {
            if (GuideTipsManager.guideTips.parent == this || GuideTipsManager.guideTips.parent == this._trackBox) 
            {
                GuideTipsManager.hideGuideTips();
            }
            return;
        }

        public showItemGuide(arg1: TrackItem): Boolean
        {
            var loc2=null;
            this.showTips = Cache.instance.role.entityInfo.level <= 15;
            var loc1=arg1.taskInfo;
            if (loc1.sTask.guide & GuideType.Fly) 
            {
                if (arg1.isClose) 
                {
                    this.guideHide();
                }
                else 
                {
                    this.taskTarget = loc1.getTargetByIndex(0);
                    if (this.taskTarget != null) 
                    {
                        this.rectImg = arg1.getImgRect();
                        this.rectTarget = arg1.getTargetRect(this.taskTarget.name);
                        this.guideTip = GuideTipsManager.getGuideTips(GuideTipsManager.Dir_TR, GuideTipsManager.Txt_Fly, this.taskTarget, loc1, this.rectImg.width + this.rectTarget.width, Math.max(this.rectImg.height, this.rectTarget.height), this.showTips, this.onTipsClick);
                        this.guideTip.x = arg1.x + arg1.bodyImgX + this.rectTarget.x + (this.rectTarget.width + this.rectImg.width) / 2;
                        this.guideTip.y = arg1.y + arg1.bodyImgY + this.rectTarget.y + this.rectTarget.height;
                    }
                    if (this.showTips) 
                    {
                        this.addChild(this.guideTip);
                    }
                    else 
                    {
                        this._scrollPane.verticalScrollPosition = 0;
                        this._trackBox.addChild(this.guideTip);
                    }
                }
                return true;
            }
            if (loc1.sTask.guide & GuideType.GuideTips) 
            {
                if (arg1.isClose) 
                {
                    this.guideHide();
                }
                else 
                {
                    this.taskTarget = loc1.getTargetByIndex(0);
                    if (this.taskTarget != null) 
                    {
                        this.rectTarget = arg1.getTargetRect(this.taskTarget.name);
                        this.guideTip = GuideTipsManager.getGuideTips(GuideTipsManager.Dir_TR, Language.getStringByParam(20707, HTMLUtil.addColor(this.taskTarget.name, "#00ff00")), this.taskTarget, loc1, this.rectTarget.width, this.rectTarget.height, this.showTips, this.onTipsClick);
                        this.guideTip.x = arg1.x + arg1.bodyImgX + this.rectTarget.x + this.rectTarget.width / 2;
                        this.guideTip.y = arg1.y + arg1.bodyImgY + this.rectTarget.y + this.rectTarget.height;
                    }
                    if (this.showTips) 
                    {
                        this.addChild(this.guideTip);
                    }
                    else 
                    {
                        this._scrollPane.verticalScrollPosition = 0;
                        this._trackBox.addChild(this.guideTip);
                    }
                }
                return true;
            }
            if (loc1.sTask.guide & GuideType.GuideCollection) 
            {
                if (arg1.isClose) 
                {
                    this.guideHide();
                }
                else 
                {
                    this.taskTarget = loc1.getTargetByIndex(0);
                    if (this.taskTarget != null) 
                    {
                        this.rectImg = arg1.getImgRect();
                        this.guideTip = GuideTipsManager.getGuideTips(GuideTipsManager.Dir_TR, GuideTipsManager.Txt_VIPFly, this.taskTarget, loc1, this.rectImg.width - 5, this.rectImg.height - 5, true, this.onTipsClick);
                        this.guideTip.x = (arg1.x + arg1.bodyImgX + this.rectImg.x + this.rectImg.width / 2 - 1);
                        this.guideTip.y = arg1.y + arg1.bodyImgY + this.rectImg.y + this.rectImg.height - 2;
                    }
                    this.addChild(this.guideTip);
                }
                return true;
            }
            if (loc1.endLevel > Cache.instance.role.entityInfo.level) 
            {
                if (arg1.isClose) 
                {
                    this.guideHide();
                }
                else if (Cache.instance.task.reachTarget.length > 0) 
                {
                    loc2 = Cache.instance.task.reachTarget[0] as TaskActInfo;
                    if (!(loc2 == null) && !(loc2.dailyInfo.target == null)) 
                    {
                        this.rectTarget = arg1.getTargetRect(loc2.dailyInfo.name);
                        this.guideTip = GuideTipsManager.getGuideTips(GuideTipsManager.Dir_TR, Language.getStringByParam(20708, HTMLUtil.addColor(loc2.dailyInfo.name, "#00ff00")), loc2.dailyInfo.target, loc1, this.rectTarget.width, this.rectTarget.height, true, this.onTipsClick, 8000);
                        this.guideTip.x = arg1.x + arg1.bodyImgX + this.rectTarget.x + this.rectTarget.width / 2;
                        this.guideTip.y = arg1.y + arg1.bodyImgY + this.rectTarget.y + this.rectTarget.height;
                    }
                    this.addChild(this.guideTip);
                }
                return true;
            }
            if (loc1.sTask.type == ETaskType._ETaskTypeApplyGuild) 
            {
                if (arg1.isClose) 
                {
                    this.guideHide();
                }
                else 
                {
                    this.taskTarget = loc1.getTargetByIndex(0);
                    if (this.taskTarget != null) 
                    {
                        this.rectTarget = arg1.getTargetRect(this.taskTarget.name, false);
                        this.guideTip = GuideTipsManager.getGuideTips(GuideTipsManager.Dir_TR, HTMLUtil.addColor(Language.getString(20709), "#00ff00"), this.taskTarget, loc1, this.rectTarget.width, this.rectTarget.height, true, this.onTipsClick);
                        this.guideTip.x = arg1.x + arg1.bodyImgX + this.rectTarget.x + this.rectTarget.width / 2;
                        this.guideTip.y = arg1.y + arg1.bodyImgY + this.rectTarget.y + this.rectTarget.height;
                    }
                    this.addChild(this.guideTip);
                }
                return true;
            }
            return false;
        }

        public get bodyHeight(): number
        {
            return this._trackBox.height;
        }

        protected /* var */_trackBox: TrackBox;

        protected /* var */_scrollPane: GScrollPane;

        protected /* var */_sortRule: Array<any>;

        /* internal */ /* var */_task: TaskInfo;

        protected /* var */taskTarget: TaskTargetInfo;

        protected /* var */guideTip: GuideTips;

        protected /* var */rectTarget: Object /* flash.geom.Rectangle */;

        protected /* var */rectImg: Object /* flash.geom.Rectangle */;

        protected /* var */showTips: Boolean;
    }
